/*
 * @Description: 标签控制层
 * @Author: fhw
 * @Date: 2024-06-15
 */

import { Controller, Get, Post, Body, Query, HttpCode, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { LogOperation } from '../operation-record/log.decorator';
import { OperateType, OperationRecordType } from '../operation-record/entities/operation-record.entity';
import { TagsService } from './tags.service';
import { CreateTagDto, UpdateTagDto, GetTagDto } from './dto/tag.dto';
import { IdDto } from 'src/common/dto/dto';
import { TagEntity } from './entities/tag.entity';

@ApiBearerAuth() // Swagger 文档设置 token
@ApiTags('标签管理')
@Controller('admin/tag')
export class TagsController {
  constructor(private readonly tagsService: TagsService) { }

  /** 获取标签列表 */
  @HttpCode(200)
  @Get('/getTagPage')
  @ApiResponse({
    status: 200,
    description: '标签列表',
    type: TagEntity,
    isArray: true,
  })
  @ApiOperation({
    tags: ['获取标签'],
    summary: '获取标签列表',
    operationId: 'getTagPage',
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async getTagPage(@Query() params: GetTagDto) {
    return this.tagsService.getTagPage(params);
  }

  /** 创建标签 */
  @HttpCode(200)
  @Post('/createTag')
  @ApiBody({
    description: '创建标签',
    type: CreateTagDto,
  })
  @ApiOperation({
    summary: '创建标签',
    operationId: 'createTag',
  })
  @LogOperation({
    operateType: OperateType.add,
    operation: `创建标签`,
    name: '创建',
    outside: OperationRecordType.tags,
    entity: TagEntity,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async createTag(@Body() body: CreateTagDto) {
    return this.tagsService.createTag(body);
  }

  /** 更新标签 */
  @HttpCode(200)
  @Post('/updateTag')
  @ApiBody({
    description: '更新标签',
    type: UpdateTagDto,
  })
  @ApiOperation({
    summary: '更新标签',
    operationId: 'updateTag',
  })
  @LogOperation({
    operateType: OperateType.edit,
    operation: `更新标签`,
    name: '更新',
    outside: OperationRecordType.tags,
    entity: TagEntity,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async updateTag(@Body() body: UpdateTagDto) {
    return this.tagsService.updateTag(body);
  }

  /** 删除标签 */
  @HttpCode(200)
  @Post('/deleteTag')
  @ApiBody({
    description: '删除标签',
    type: IdDto,
  })
  @ApiOperation({
    summary: '删除标签',
    operationId: 'deleteTag',
  })
  @LogOperation({
    operateType: OperateType.delete,
    operation: `删除标签`,
    name: '删除',
    outside: OperationRecordType.tags,
    entity: TagEntity,
  })
  @UseGuards(AuthGuard('jwt')) // 自定义守卫
  async deleteTag(@Body() body: IdDto) {
    return this.tagsService.deleteTag(body);
  }
}
